<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class VideoSearch extends Video {

    public $query;
    public $filterServer;
    public $filterCategory;
    public $filterRegion;
    public $filterPlayer;
    public $filterDialog;
    public $filterLevel;

    public function rules() {
        return array(
            array('query,filterRegion,filterPlayer,filterDialog', 'length'),
            array('filterServer,filterCategory,filterLevel', 'numerical', 'min' => 1),
        );
    }

    public function filterServer() {
        $list = Server::model()->findAll();
        $data = CHtml::listData($list, 'server_id', 'server_name');
        return array_merge_recursive(array('全部服务器'), $data);
    }

    public function filterCategory() {
        $list = Category::model()->findAll(array('order' => 'category_id desc'));
        $data[] = Yii::t('default', '全部视频分类');
        if ($list) {
            foreach ($list as $var) {
                $data[$var['category_id']] = '' . $var['category_name'];
            }
        }
        return $data;
    }

    public function filterLevel() {
        $usergroup = UserGroup::model()->findAll(array('order' => 'usergroup_level ASC'));
        $data = array();
        $data[] = Yii::t('default', '全部权限视频');
        if ($usergroup) {
            foreach ($usergroup as $user) {
                $data[$user['usergroup_level']] = $user['usergroup_name'];
            }
        }
        return $data;
    }

    public function filterRegion() {
        $list = $this->getRegionArray();
        return array_merge(array('全部地区'), $list);
    }

    public function filterPlayer() {
        $list = Player::model()->findAll();
        $data = CHtml::listData($list, 'player_alias', 'player_name');
        return array_merge(array('全部播放器'), $data);
    }

    public function filterDialog() {
        $list = $this->getDialogArray();
        return array_merge(array('全部语种'), $list);
    }

    /**
     * 搜索数据
     * @return CActiveDataProvider 
     */
    public function getSearch() {
        $criteria = new CDbCriteria();
        $criteria->with = array('videoCateroy', 'videoServer', 'videoRegion');
        if ($this->query) {
            $criteria->addSearchCondition('video_name', $this->query, true);
            $criteria->addSearchCondition('video_alias', $this->query, true, 'OR');
            $criteria->addSearchCondition('video_dialog', $this->query, true, 'OR');
        }
        if ($this->filterServer) {

            $criteria->addCondition('videoServer.server_id=' . $this->filterServer);
        }
        if ($this->filterCategory) {

            $criteria->addCondition('videoCateroy.category_id=' . (int) $this->filterCategory);
        }
        if ($this->filterDialog) {
            $criteria->addSearchCondition('video_dialog', $this->filterDialog);
        }
        if ($this->filterRegion) {
            $criteria->addCondition('videoRegion.region_name = :name');
            $criteria->params[':name'] = $this->filterRegion;
        }
        if ($this->filterPlayer) {
            $criteria->addSearchCondition('video_player_alias', $this->filterPlayer);
        }
        if ($this->filterLevel) {
            $criteria->addCondition('video_play_level = :level1');
            $criteria->params[':level1'] = $this->filterLevel;
            $criteria->addCondition('video_down_level = :level2', 'OR');
            $criteria->params[':level2'] = $this->filterLevel;
        }
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => (int)Config::model()->getConfig('blackend_page_size'),
                    ),
                    'sort' => array(
                        'defaultOrder' => 't.video_id DESC', //设置默认排序是create_time倒序
                    ),
                ));
    }

}
